'use strict';
var utils = require('../utils'),
    pipeline = require('../../utils/pipeline'),

    docName = 'users',
    allowedIncludes = ['count.posts', 'permissions', 'roles', 'roles.permissions'];

module.exports = app => {
    class User extends app.Service {
        * find(options) {
            var attrs = ['id', 'slug', 'status', 'email', 'role'],
                tasks;
            function doQuery(options) {
                return app.mysql.get('users', {id: options.id});
            }

            tasks = [
                //utils.validate(docName, {attrs: attrs}),
                //utils.handlePublicPermissions(docName, 'read'),
                //utils.convertOptions(allowedIncludes),
                doQuery
            ];
            return pipeline(tasks, options).then(function formatResponse(result) {
                if (result) {
                    return utils.reqsuccess({data:result});
                }
                return utils.reqfailed({msg:"未找到此用户"});
            })
        }

        * findAll(options) {
            var attrs = ['id', 'slug', 'status', 'email', 'role'],
                tasks;
            function doQuery(options) {
                return app.mysql.select('users');
            }

            tasks = [
                //utils.validate(docName, {attrs: attrs}),
                //utils.handlePublicPermissions(docName, 'read'),
                //utils.convertOptions(allowedIncludes),
                doQuery
            ];
            return pipeline(tasks, options).then(function formatResponse(result) {
                if (result) {
                    return {users: result};
                }
            })
        }

        * add(options) {
            var attrs = ['id', 'slug', 'status', 'email', 'role'],
                tasks;
            function doQuery(options) {
                return app.mysql.insert('users',options);
            }

            tasks = [
                //utils.validate(docName, {attrs: attrs}),
                //utils.handlePublicPermissions(docName, 'read'),
                //utils.convertOptions(allowedIncludes),
                doQuery
            ];
            return pipeline(tasks, options).then(function formatResponse(result) {
                if (result) {
                    return {users: result};
                }
            })
        }

        * edit(options) {
            var attrs = ['id', 'slug', 'status', 'email', 'role'],
                tasks;
            function doQuery(options) {
                return app.mysql.update('users',options);
            }

            tasks = [
                //utils.validate(docName, {attrs: attrs}),
                //utils.handlePublicPermissions(docName, 'read'),
                //utils.convertOptions(allowedIncludes),
                doQuery
            ];
            return pipeline(tasks, options).then(function formatResponse(result) {
                if (result.affectedRows === 1) {
                    return {users: result};
                }
            })
        }
        * delete(options) {
            var attrs = ['id', 'slug', 'status', 'email', 'role'],
                tasks;
            function doQuery(options) {
                return app.mysql.delete('users',options);
            }

            tasks = [
                //utils.validate(docName, {attrs: attrs}),
                //utils.handlePublicPermissions(docName, 'read'),
                //utils.convertOptions(allowedIncludes),
                doQuery
            ];
            return pipeline(tasks, options).then(function formatResponse(result) {
                if (result.affectedRows === 1) {
                    return {users: result};
                }
            })
        }
    }
    return User;
};
